package com.example.zuo.mysuperwidgetdemo.adapter.superwrapper;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.HashMap;

/**
 * Created by a1800 on 4/3/2017.
 */
public class Builder {


    WrapperParams params;
    HashMap<String, BaseAction> actionMap;

    public Builder(Context context) {
        params = new WrapperParams();
        params.setContext(context);
    }

    /**
     * 设置被包裹的RecyclerView
     * 必须配置
     *
     * @param view RecyclerView
     * @return builder
     */
    public Builder showIn(RecyclerView view) {
        params.setrView(view);
        return this;
    }

    /**
     * 设置SwipeRefreshLayout用于下拉刷新
     * 不配置则不含刷新功能
     *
     * @param layout SwipeRefreshLayout
     * @return builder
     */
    public Builder refreshBy(SwipeRefreshLayout layout) {
        params.setsLayout(layout);
        return this;
    }


    /**
     * 添加动作
     *
     * @param action
     * @return builder
     */
    public Builder addAction(String command, BaseAction action) {
        if (actionMap == null)
            actionMap = new HashMap<>();
        actionMap.put(command, action);
        return this;
    }

    /**
     * 为RecyclerView设置LayoutManager
     * 不设置则默认为LinearLayoutManager竖直方向
     *
     * @param manager layotuManager
     * @return builder
     */
    public Builder manageRView(RecyclerView.LayoutManager manager) {
        params.setManager(manager);
        return this;
    }

    /**
     * 为默认adapter设置布局
     * 如果才用默认CommentAdapter则为必须配置选项
     *
     * @param res 布局id
     * @return builder
     */
    public Builder defalutItemLayout(int res) {
        params.setItemLayoutRes(res);
        return this;
    }
    /**
     * 为默认adapter设置分割线
     * @param item 分割线
     * @return builder
     */
    public Builder addItemDecoration(RecyclerView.ItemDecoration item){
        params.setItem(item);
        return this;
    }


    @SuppressWarnings("unchecked")
    public SuperAdapterWrapper build() {
        SuperAdapterWrapper wrapper = new SuperAdapterWrapper<>();
        wrapper.context = params.getContext();
        wrapper.rView = params.getrView();
        wrapper.sLayout = params.getsLayout();
        wrapper.item=params.getItem();
        if (params.getManager() != null) {
            wrapper.manager = params.getManager();
        } else {
            LinearLayoutManager manager = new LinearLayoutManager(params.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            wrapper.manager = manager;
        }

        if (params.getItemLayoutRes() != 0) {
            wrapper.itemLayoutRes = params.getItemLayoutRes();

        } else {
            wrapper.adapterType = SuperAdapterWrapper.CUSTOM_MODE;
        }

        wrapper.actionMap = actionMap;
        wrapper.init();
        return wrapper;
    }
}